<template>
  <div class="report-form-content">
    <el-form class="my-form"
             ref="objForm"
             :model="obj"
             :rules="rules"
             label-position="right"
             label-width="auto"
             label-suffix="："
             :disabled="oper === 'view' || oper === 'supplement'">
      <div class="tabs-form-head">
        <span>合同跟踪</span>
        <div class="">
          <el-button type="primary" plain @click="returnBtn">取消</el-button>
          <el-button type="primary" @click="doSubmit">确认添加</el-button>
        </div>
      </div>
      <el-divider border-style="dashed" />
      <el-row :gutter="20">
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
            <div class="report-form-list-content-1">
              <el-form-item label="合同负责人">
                <LabelTag :foreignList="userList" :foreignIds="principalUserId" :foreignLabel="'name'" />
              </el-form-item>
            </div>
            <div class="report-form-list-content">
              <el-form-item label="新增合同跟踪人员" prop="archiveUserId">
                <User v-model="obj.archiveUserId" keyName="id" keyShow="name" placeholder="合同跟踪人员" title="用户查询"
                      is-multiple
                      @onchange="archiveUserIdChange" />
              </el-form-item>
            </div>
            <div class="report-form-list-content-1">
              <el-form-item label="合同已添加跟踪人员">
                <el-tag v-for="item in archiveUserId"
                        :key="item"
                        closable
                        class="tags"
                        :disable-transitions="false"
                        @close="handleClose(item)">
                  {{ showIdLabel(userList, item, 'name') }}
                </el-tag>
              </el-form-item>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script setup>
import {ref, getCurrentInstance} from 'vue'
import {doGet, doPost} from "@/config/http";
import {svc, showIdLabel, getListPageSize, notifySuccess, alertErrorMsg} from "@/config/common";
import { useRoute } from 'vue-router'
import User from "@/views/components/base/selectUser.vue";
import initListStore from '@/store/modules/initList'

const route = useRoute()

const { proxy: ctx } = getCurrentInstance();

let archiveUserId = ref([])
let principalUserId = ref('')

let objForm = ref(null)
const oper = ref(route.query.oper)

let obj = ref( {
  contractInitialId: route.query.id, // 合同ID
  batch: null, // 批次号
  archiveUserId: null, // 跟踪人
  archiveDepartmentId: null, // 跟踪人部门
  remark: null, // 备注
  id: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  deleted: null,
  version: null,
})

// let objList = ref([])

// let contractTypeList = ref([]) // 合同类型
// let contractCooperationList = ref([]) // 往来单位
// let contractFeetypeList = ref([]) // 合同费用类型
// let contractTemplateList = ref([]) // 合同模版
// let bankList = ref([]) // 银行
let userList = initListStore().userList // 用户
// let governmentList = ref([]) //  政府单位
// let organizationList = ref([]) // 部门
// let contractSignerList = ref([]) // 签订人
// let contractFiletypeList = ref([]) // 合同附件类型

const rules = {}

// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型

//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }

const initObj = async () =>  {
  if (route.query.id) {
    let contractInitial = await doGet(svc, '/contract-initial/' + route.query.id, {}, true)
    console.log('contractInitial-----------',contractInitial)
    principalUserId.value = contractInitial.userId
  }
}

// initList()
initObj()

const archiveUserIdChange = (val) => {
  val.map(item => {
    if (!archiveUserId.value.includes(item.id)) {
      archiveUserId.value.push(item.id)
    }
  })
}

const handleClose = (item) => {
  archiveUserId.value.splice(archiveUserId.value.indexOf(item), 1)
}

const doSubmit = async () => {
  let v = await objForm.value.validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  if (archiveUserId.value.length <= 0) {
    alertErrorMsg('请添加追踪人。')
    return
  }
  let o = {}
  o.oper = 'add'
  o.approveId = null
  o.approveNodes = []
  o.items = archiveUserId.value.map(item => ({archiveUserId: item, contractInitialId: route.query.id}))
  doPost(svc, `/customContractInitial/trackingContractInitial/${route.query.id}`, o, true).then(() => {
    notifySuccess('操作成功')
    returnBtn()
  })
}

const returnBtn = () => {
  switch (route.query.kind) {
    case '1':
      ctx.goTargetByName('contractInitial', {});
      break;
    case '2':
      ctx.goTargetByName('contractMain', {})
      break;
  }
}
</script>

<style scoped>
.report-form-content {
  padding: 20px;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
.tags {
  margin: 0 5px
}
</style>
